package android.slc.main_biz.ui.main.text;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.slc.commonlibrary.util.ZoomUtils;
import android.slc.main_biz.ui.main.base.BaseItem;
import android.slc.main_biz.ui.main.curve.FastOutSlowInCurve;
import android.view.animation.Interpolator;

import androidx.interpolator.view.animation.FastOutSlowInInterpolator;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.SizeUtils;

import java.util.Random;


/**
 * Created by LinGuanHong on 2017/1/16.
 */

public class TextItem extends BaseItem {
    public final FastOutSlowInCurve fastOutSlowInCurve = new FastOutSlowInCurve();
    private PointF center;
    private int radius;
    private float vx;
    private float vy;
    private final Random random = new Random();
    private Paint paint;

    private Bitmap textBmp;

    public TextItem(int width, int height, Resources resources) {
        super(width, height, resources);
        init();
        loopInit();
    }

    private void init() {
        paint = new Paint();
        //paint.setColor(getColor());
        radius = SizeUtils.dp2px(20);
        loopInit();
    }

    public void setTextBmp(Bitmap textBmp) {
        float maxSize = Math.max(textBmp.getWidth(), textBmp.getHeight());
        float proportion = radius*2 / maxSize;

        this.textBmp = ImageUtils.scale(textBmp, proportion, proportion);
    }

    public Bitmap getTextBmp() {
        return textBmp;
    }

    private void loopInit() {
        center = new PointF(random.nextInt(Math.round(width - radius / 2f)) + radius / 2f, random.nextInt(Math.round(height - radius / 2f)) + radius / 2f);
        //卡墙纠正
        if (center.x - radius < 0) {
            center.x += radius;
        }
        if (center.y - radius < 0) {
            center.y += radius;
        }
        vx = random.nextInt(2) + 2;
        if (random.nextBoolean()) {
            vx = -vx;
        }
        vy = random.nextInt(10) + 2;
        if (random.nextBoolean()) {
            vy = -vy;
        }
    }

    @Override
    public void draw(Canvas canvas) {
        //canvas.drawCircle(center.x, center.y, radius, paint);
        canvas.drawBitmap(textBmp, center.x-radius, center.y-radius, paint);
    }

    @Override
    public void move() {
        if (vx != 0 || vy != 0) {
            float interpolation = fastOutSlowInCurve.getInterpolation();
            System.out.println(interpolation);
            center.x += vx;
            //center.x += vx * interpolation;
            //center.y += vy;
            center.y += vy * 2 * interpolation;
            /*if(center.y+radius>height){
                fastOutSlowInCurve.reset();
                center.y = center.y+radius;
            }else if(center.y-radius>0){

            }*/
        }
        setCenter(center); //重置圆心
        //interferenceSpeed(this);//干扰速度
        boundChech(this);
    }

    public void setCenter(PointF center) {
        this.center = center;
    }

    public void setCenter(float x, float y) {
        this.center.x = x;
        this.center.y = y;
    }

    public PointF getCenter() {
        return center;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public float getVx() {
        return vx;
    }

    public void setVx(float vx) {
        this.vx = vx;
    }

    public float getVy() {
        return vy;
    }

    public void setVy(float vy) {
        this.vy = vy;
    }

    /** 球体碰撞在这里处理 */
    /** 边界碰撞交给 item */
    /** 细分有：(也是算法优化)
     *     纯垂直
     *     纯水平
     *     纯切面
     * */
    /**
     * r1+r2 > 开方(x1-x2)^2+(y2-y1)^2
     */
    /*public boolean collide(TextItem b1) {
        if (getVx() != 0 || b1.getVx() != 0 || getVy() != 0 || b1.getVy() != 0) {
            double xSquare = Math.pow(center.x - (b1.center.x), 2);
            double ySquare = Math.pow(center.y - (b1.center.y), 2);
            double radiusSquare = Math.pow(radius + b1.getRadius(), 2);
            double distanceSquare = xSquare + ySquare;
            return distanceSquare <= radiusSquare;
        } else {
            return false;
        }
    }*/
    private int getColor() {
        return 0xff000000 | random.nextInt(0x00ffffff);//没透明度
    }

    //墙体
    private void boundChech(TextItem b1) {
        //加减5解决撞墙误差
        if (b1.getCenter().x + b1.getRadius() + b1.getVx() >= width - 1) {
            b1.getCenter().x -= 2;
            b1.setVx(-1 * b1.getVx());
        }
        if (b1.getCenter().x + b1.getVx() - b1.getRadius() <= 1) {
            b1.getCenter().x += 2;
            b1.setVx(-1 * b1.getVx());
        }
        if (b1.getCenter().y + b1.getRadius() + b1.getVy() >= height - 1) {
            b1.getCenter().y -= 2;
            b1.setVy(-1 * b1.getVy());
            fastOutSlowInCurve.reset();
        }
        if (b1.getCenter().y + b1.getVy() - b1.getRadius() <= 1) {
            b1.getCenter().y += 2;
            b1.setVy(-1 * b1.getVy());
            fastOutSlowInCurve.reset();
        }
    }

}